<script>
	import config from './utils/config.js';
	import request from './utils/request.js';
	export default {
		onLaunch: function() {
			let _this  = this
			uni.getStorage({
				key: 'userLogin',
				success(r) {
					_this.globalData.hasLogin = true
				},
				
			})
			this.getAdInfo()
			this.getShareId();//获取分享信息-kkkgogogo
			this.checkSession(openid => {//获取用户openid-gogokkk
				this.globalData.openid = openid;
				//获取当前用户可看广告的数据
				this.getAdLogInfo()
				this.saveAnalysisInfo(1)
			},this.globalData.projectId);
			
			
		},
		onShow: function() {
			
		},
		onHide: function() {
			
		},
		methods:{
			// 获取分享信息
			async getShareId() {
				let _this  = this
				const r = await request({
					url:  `${config.API}api/w/getShareInfo`,
					method: 'POST'
				});
				if (r.error_code === 0) {
					_this.globalData.shareInfo = r.data
				}
			},
			
			//检查openid
			checkSession(callback,projectId){
				let _this = this
				uni.checkSession({
					success(r) {
						uni.getStorage({
							key: 'openid',
							success(r) {
								if (!r.data) {
									_this.loginAction(callback,projectId);
								} else {
									callback && callback(r.data);
								}
							},
							fail(err) {
								_this.loginAction(callback,projectId);
							}
						})
					},
					fail(err) {
						_this.loginAction(callback,projectId);
					}
				});
				// _this.loginAction(callback,projectId);
			},
			//获取openid
			loginAction(callback,projectId) {
				uni.login({
					async success(r) {
						uni.request({
							url: `${config.API}api/user/openid/bind`,
							data: {
								code: r.code,
								projectId:projectId
							},
							success(r2) {
								if (r2.data.error_code === 0) {
									uni.setStorage({
										key: 'openid',
										data: r2.data.data.openid
									});
									callback && callback(r2.data.data.openid);
								} else {
									uni.showToast({
										title: '获取openid失败',
										icon: 'none'
									});
								}
							},
							fail(err) {
								console.log(err);
							}
						});
					}
				});
			},
			//存储用户信息
			async saveUserInfo(avatarUrl,nickName) {
				const r = await request({
					url: `${config.API}api/user/saveInfo`,
					method:'POST',
					data: {
						openid: this.globalData.openid,
						avatarUrl:avatarUrl,
						nickName:nickName,
						platform:this.globalData.projectId
					},
				});
				console.log(r) 
			},
			//获取广告
			getAdInfo(){
				let _this = this
				uni.request({
					url: `${config.API}api/w/getAdInfo`,
					data: {
						projectId:this.globalData.projectId
					},
					method:'POST',
					success(res) {
						_this.globalData.adInfo = res.data.data
						_this.globalData.canShowAd = res.data.data.open_ad == 1?true:false
						console.log(_this.globalData.adInfo)
					}
				})
			},
			
			// 获取看广告信息
			async getAdLogInfo() {
				let _this  = this
				const r = await request({
					url:  `${config.API}api/ad/logSearch`,
					method: 'POST',
					data:{
						openid:this.globalData.openid,
						platform:this.globalData.projectId
					}
				});
				_this.globalData.ad_number = r.data.ad_number
				_this.globalData.illegal_ad_number = r.data.illegal_ad_number
				_this.globalData.free_ad_num = r.data.free_ad_num
				_this.globalData.illfree_ad_num = r.data.illfree_ad_num
				_this.globalData.de_ad_number = r.data.de_ad_number
				_this.globalData.adRequestRes = true
				
			},
			
			// 增加看广告信息
			async addAdLog() {
				let _this  = this
				const r = await request({
					url:  `${config.API}api/ad/logAdd`,
					method: 'POST',
					data:{
						openid:this.globalData.openid,
						platform:this.globalData.projectId
					}
				});

				_this.globalData.illegal_ad_number++
			},
			// 增加免费不看广告信息
			async addFreeAdLog() {
				let _this  = this
				const r = await request({
					url:  `${config.API}api/ad/logAdd`,
					method: 'POST',
					data:{
						openid:this.globalData.openid,
						platform:this.globalData.projectId,
						type:2,
					}
				});
			
				_this.globalData.illfree_ad_num++
			},
			
			
			//存储数据埋点分析内容
			//type : 具体埋点位置内容
			 // 1=>'进入小程序',2=>'进入素材详情页',3=>'点击看广告下载的次数',4=>'点击普通下载的次数'
			 //        ,5=>'真实看完广告的次数',6=>'未正常看完广告的次数',7=>'广告加载异常次数',
			async saveAnalysisInfo(analysis_type,wallpaperId=0) {
				const r = await request({
					url: `${config.API}api/w/data/analysis/save`,
					method:'POST',
					data: {
						openid: this.globalData.openid,
						platform:this.globalData.projectId,
						type:analysis_type,
						wallpaper_id:wallpaperId
					},
				});
				console.log(r) 
			},
			
			//处理看广告分销功能
			async dealPp(id,nickName,avatarUrl) {
				const r = await request({
					url: `${config.API}api/w/dealPp`,
					method: 'POST',
					data: {
						id: id,
						platform: this.globalData.projectId,
						openid: this.globalData.openid,
						nickname: nickName,
						avatar_url: avatarUrl,
					}
				});
			
			},
		},
		globalData:{
			//环境区分类型
			// #ifdef MP-QQ
			projectId:'qq',
			// #endif
			// #ifdef MP-WEIXIN
			projectId:'wx',
			// #endif
			// #ifdef MP-TOUTIAO
			projectId:'dy',
			// #endif
			// #ifdef MP-KUAISHOU
			projectId:'ks',
			// #endif
			canShowAd : false,//是否能显示广告组件
			
			
			
			shareInfo:null,//分享信息
			adInfo:null,//广告信息
			openid:'',//用户openid
			hasLogin:false,//是否登录了
			
			ad_number:0,//记录可看广告次数
			illegal_ad_number:0,//记录已看广告次数
			free_ad_num:0,//免费赠送的广告下载次数
			illfree_ad_num:0,//已使用免费下载次数
			de_ad_number:0,//原始可看广告次数
			adRequestRes:false,//请求当前用户看广告的响应
		}
	}
</script>

<style>
	image {
		height: auto;
	}
	
	.loading {
		display: block;
		padding: 20rpx 0;
		text-align: center;
		color: #999;
		font-size: 28rpx;
	}
	
	.loading-bg {
		position: relative;
	}
	.loading-bg image {
		position: relative;
		z-index: 1;
	}
	.loading-bg:before {
		animation: loadingAnimation infinite 1s;
		display: block;
		content: '';
		width: 60rpx;
		height: 60rpx;
		position: absolute;
		top: 50%;
		left: 50%;
		z-index: 0;
		margin: -30rpx 0 0 -30rpx;
		// background: url('') no-repeat 100%/100%;
	}
	
	.loading-bg.white:before {
		width: 100rpx;
		height: 100rpx;
		margin: -50rpx 0 0 -50rpx;
		// background: url('') no-repeat 100%/100%;
	}
	
	@keyframes loadingAnimation{
		from {
			transform: rotate(0);
		}
		to {
			transform: rotate(360deg);
		}
	}
	
	.label-rel {
		position: relative;
	}
	.label-gif {
		position: absolute;
		z-index: 9;
		background: rgba(0, 0, 0, .6);
		color: white;
		font-size: 24rpx;
		right: 0;
		bottom: 0;
		font-weight: bold;
		padding: 0 14rpx;
		border-top-left-radius: 12rpx;
		border-bottom-right-radius: 12rpx;
	}
</style>
